package com.sirui.jpa.web;

import com.sirui.jpa.model.JpaStudent;
import com.sirui.jpa.model.Student;
import com.sirui.jpa.service.IStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "测试接口")
@RestController
@RequestMapping("/test")
@Slf4j
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class StudentController {
    private final static Logger LOGGER = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private IStudentService studentService;

    @ApiOperation(value = "学生List")
    @PostMapping("/list")
    public Result list(HttpServletRequest request) {
        LOGGER.info("学生List");
        List<Object[]> list = studentService.listStudent();
        return Result.ok(list);
    }

    @ApiOperation(value = "listByPage")
    @PostMapping("/listByPage")
    public Result listByPage(HttpServletRequest request, @RequestParam int pageNum, @RequestParam int pageSize) {
        LOGGER.info("listByPage");
        log.info("A@@A [{},{},{}]", pageNum, pageSize, LocalDateTime.now());
        List<JpaStudent> list = studentService.listStudent(pageNum, pageSize);
        log.info("A@@A [{}]", LocalDateTime.now());
        return Result.ok(list);
    }

    @ApiOperation(value = "学生Map")
    @PostMapping("/listMap")
    public Result listMap(HttpServletRequest request) {
        LOGGER.info("学生Map");
        List<Map<Object, Object>> list = studentService.listStudentMap();
        return Result.ok(list);
    }

    @ApiOperation(value = "学生Model")
    @PostMapping("/listModel")
    public Result listModel(HttpServletRequest request) {
        LOGGER.info("学生Model");
        List<Student> list = studentService.listStudentModel();
        return Result.ok(list);
    }

    @ApiOperation(value = "学生APP GET")
    @GetMapping("testGet")
    public Result find(HttpServletRequest request, @RequestParam(value = "id", required = false) Integer id) {
        return Result.ok(id);
    }

    @ApiOperation(value = "学生APP POST")
    @PostMapping("testPost")
    public Result save(HttpServletRequest request, @RequestBody Map<String, Object> map, @RequestParam("id") Integer id) {
        LOGGER.info("学生APP");
        log.info("A@A:{}", id);
        log.info("A@A:{}", map);
        ArrayList<Object> list = new ArrayList<Object>();
        list.add(id);
        list.add(map);
        return Result.ok(list);
    }

    @ApiOperation(value = "学生APP POST1")
    @PostMapping("testPost1")
    public Result testPost1(HttpServletRequest request, @RequestParam("id") Integer id, @RequestBody(required = false) Map<String, Object> map) {
        LOGGER.info("学生APP");
        List<Student> list = studentService.listStudentModel();
        log.info("A@A:{}", id);
        log.info("A@A:{}", map);
        return Result.ok(id);
    }

    @ApiOperation(value = "学生APP POST2")
    @PostMapping("testPost2")
    public Result testPost2(HttpServletRequest request, @RequestParam("id") Integer id) {
        LOGGER.info("学生APP");
        List<Student> list = studentService.listStudentModel();
        log.info("A@A:{}", id);
        return Result.ok(id);
    }
}
